package com.model.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.model.service.admin.AdminService;
import com.model.service.student.StudentService;
import com.model.service.teacher.TeacherService;
import com.model.vo.Admin;
import com.model.vo.Student;
import com.model.vo.Teacher;


@SuppressWarnings("serial")
public class LoginAction extends BaseAction implements SessionAware{

	@SuppressWarnings("rawtypes")
	Map session;
	Admin admin;
	String checkCode;
	String message;
  @Autowired
  AdminService adminService;
  @Autowired
  StudentService studentService;
  @Autowired
 TeacherService teacherService;  //涓氬姟灞�
  String resultView;   //瑙嗗浘椤甸潰
   /**
    * 鐧诲綍椤甸潰
    */
	@Override
	public String execute() throws Exception {
		
		return INPUT;
	}
	/**
	 * 鐧诲綍鏍￠獙
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String isLogin() {
		if(session.get("checkCode")!=null&&!session.get("checkCode").equals(checkCode)){
			resultView = "/admin/login.action";
	    	return SUCCESS;
		}
		if(admin!=null){
			
	    if(admin.getRole().equals("0")){ //瀛︾敓鐧诲綍
	    	List<Student> list = studentService.
	    			QueryByHqlList("from Student as a where a.stuName=? and a.password=?",
	    					new Object[]{admin.getName(),admin.getPassword()});
	    	System.out.println(list);
			if(list.size()>0){
				resultView = "/admin/login_LoginToMain.action";
				session.put("name", admin.getName());
				session.put("role","1");
				session.put("nameId",list.get(0).getId());
				session.put("roleName","student");
				return SUCCESS;
			}
	    }else  if(admin.getRole().equals("1")||admin.getRole().equals("2")||admin.getRole().equals("3")
	    		){ //鑰佸笀,鏍￠瀵硷紝瀛﹀伐澶勭櫥褰�
	    	List<Teacher> list1 = teacherService.
	    	QueryByHqlList("from Teacher as a where a.teachName=? and a.password=?",
	        new Object[]{admin.getName(),admin.getPassword()});
			if(list1.size()>0){
				resultView = "/admin/login_LoginToMain.action";
				session.put("name", admin.getName());
				session.put("nameId",list1.get(0).getId());
				session.put("roleName","teacher");
				session.put("role",admin.getRole()+"2");
				return SUCCESS;
			}
	    }else  if(admin.getRole().equals("4")){ //绠＄悊鍛樼櫥褰�
	    	List<Admin> list3 = adminService.
	    			QueryByHqlList("from Admin as a where a.name=? and a.password=?",
	    					new Object[]{admin.getName(),admin.getPassword()});
			if(list3.size()>0){
				resultView = "/admin/login_LoginToMain.action";
				session.put("name", admin.getName());
				session.put("role","4");
				session.put("roleName","sys");
				session.put("nameId",list3.get(0).getId());
				return SUCCESS;
			}
	    }
	    
	}
		resultView = "/admin/login.action";
    	return SUCCESS;

	}
    /**
     * 鐧诲綍鎴愬姛鍘讳富椤�
     * @return
     */
	public String LoginToMain() {
    	if(session.get("name")==null){
    	resultView = "/admin/login.action";
    	}
		return "main";
	}
	/**
	 * 閫�嚭鐧诲綍
	 */
	public String  outLogin() {
	
    	session.remove("name");
		session.remove("role");
		session.remove("roleName");
		session.remove("nameId");
    	resultView = "/admin/login.action";
		return SUCCESS;
    	
	}
	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public AdminService getAdminService() {
		return adminService;
	}
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	public String getResultView() {
		return resultView;
	}
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}
	public Map getSession() {
		return session;
	}
	public StudentService getStudentService() {
		return studentService;
	}
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	public String getCheckCode() {
		return checkCode;
	}
	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public TeacherService getTeacherService() {
		return teacherService;
	}
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	
}
